﻿/*----------------------------------------------------
 * Author : Tharinduni Udugamasuriya
 * Date : 04th August 2014
 * RelayCommand : To handel Commands in MVVM 
 ---------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace DSPL.Contracts.Command
{
    public class RelayCommand : ICommand
    {
        readonly Action<object> execute;
        readonly Predicate<object> canExecute;

        public RelayCommand(Action<object> _execute)
            : this(_execute, null)
        {

        }

        public RelayCommand(Action<object> _execute, Predicate<object> _canExecute)
        {
            this.execute = _execute;
            this.canExecute = _canExecute;
        }

        public bool CanExecute(object parameter)
        {
            if (this.canExecute != null)
            {
                return this.canExecute(parameter);
            }
            else
            {
                return true;
            }
        }

        public void Execute(object parameter)
        {
            if (this.execute != null)
            {
                this.execute(parameter);
            }
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }


    }
}
